package com.name.givename.utils;

import com.alibaba.fastjson.JSONObject;
import com.name.givename.entity.WeiXinEntity;
import com.name.givename.share.Sign;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;

/**
 * Created by yangtianrui on 2018/9/3.
 */
@Service
public class WeXinUtil {

    public static WeiXinEntity getWeiXinEntity(String url){

        WeiXinEntity weiXinEntity = new WeiXinEntity();
        String access_token = getAccessToken();
        String  ticket = getTicket(access_token);
        System.out.println(ticket+">>>>>>>>>>>>>>>>>");
        Map<String,String > ret = Sign.sign(ticket,url);
        System.out.println(ret.toString());
        weiXinEntity.setTicket(ret.get("jsapi_ticket"));
        weiXinEntity.setSignature(ret.get("signature"));
        weiXinEntity.setNoncestr(ret.get("nonceStr"));
        weiXinEntity.setTime_stamp(ret.get("timestamp"));
        System.out.println(weiXinEntity.toString()+"<<<<<<<<<<<<<<<<<<<<<<");
        return weiXinEntity;
    }

    // 返回token
    public static String getAccessToken() {
        String accessToken = "";
        String grant_tpe = "client_credential";
        String AppId = "123";
        String secret="113";

        String url ="https://api.weixin.qq.com/cgi-bin/token?grant_type="+grant_tpe+"&appid="+AppId+"&secret="+secret;

        try {
            URL urlGet = new URL(url);
            HttpURLConnection http = (HttpURLConnection) urlGet.openConnection();
            http.setRequestMethod("GET"); // 必须是get方式请求
            http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
            http.setDoOutput(true);
            http.setDoInput(true);
            /*System.setProperty("sun.net.client.defaultConnectTimeout", "30000");// 连接超时30秒
            System.setProperty("sun.net.client.defaultReadTimeout", "30000"); // 读取超时30秒 */
            http.connect();
            InputStream is = http.getInputStream();
            int size = is.available();
            byte[] jsonBytes = new byte[size];
            is.read(jsonBytes);
            String message = new String(jsonBytes, "UTF-8");

            //JSONObject demoJson = JSONObject.fromObject(message);
            JSONObject demoJson = JSONObject.parseObject(message);
            accessToken = demoJson.getString("access_token");
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return accessToken;
    }
//  获取ticket
    public static String getTicket(String access_token) {
        String ticket = null;
        String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token="+ access_token +"&type=jsapi";//这个url链接和参数不能变
        try {
            URL urlGet = new URL(url);
            HttpURLConnection http = (HttpURLConnection) urlGet.openConnection();
            http.setRequestMethod("GET"); // 必须是get方式请求
            http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
            http.setDoOutput(true);
            http.setDoInput(true);
            System.setProperty("sun.net.client.defaultConnectTimeout", "30000");// 连接超时30秒
            System.setProperty("sun.net.client.defaultReadTimeout", "30000"); // 读取超时30秒
            http.connect();
            InputStream is = http.getInputStream();
            int size = is.available();
            byte[] jsonBytes = new byte[size];
            is.read(jsonBytes);
            String message = new String(jsonBytes, "UTF-8");
//            JSONObject demoJson = JSONObject.fromObject(message);
            JSONObject demoJson = JSONObject.parseObject(message);
            ticket = demoJson.getString("ticket");
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ticket;
    }


}
